<?php
add_action('wp_head', 'active_coupon');
function active_coupon(){ ?>
<script type="text/javascript">
            jQuery(document).ready(function($){
                $('a.coupon_title').each(function(){
                    $(this).click(function(){
                       var parent = $(this).closest('.data-cp');
                       var cp_id = parent.attr('id');
                       var current_url = parent.data('current_url').split('?');
                       // Added 
                       var url_popup = '';
                       if(!current_url[1])
                            url_popup = current_url[0]+'?c='+cp_id;
                       else
                            url_popup = '?'+current_url[1]+'&c='+cp_id;
                       // End add
                       $(this).attr('href', url_popup);
                       
                       var st_id = $(this).attr('target');
                       var data ={
                            action : 'get_aff',
                            value_send : st_id
                        };
                       jQuery.post(ajaxurl, data, function(result) {
                            window.location = result;
                        });
                    });
                });
                $('.action-button').each(function(){
                    $(this).click(function(){
                        var parent = $(this).closest('.data-cp');
                        var cp_id = parent.attr('id');
                        var current_url = parent.data('current_url').split('?');
                        // Added 
                       var url_popup = '';
                       if(!current_url[1])
                            url_popup = current_url[0]+'?c='+cp_id;
                       else
                            url_popup = '?'+current_url[1]+'&c='+cp_id;
                       // End add
                        var st_id = parent.data('store');
                        window.open(url_popup);
                        var data ={
                            action : 'get_aff',
                            value_send : st_id
                        };
                       jQuery.post(ajaxurl, data, function(result) {
                            window.location = result;
                        });
                    });
                });
            });
</script>
<?php }
add_action('wp_ajax_nopriv_get_aff', 'get_aff_callback');
add_action('wp_ajax_get_aff', 'get_aff_callback');
function get_aff_callback(){
    $value_received = $_POST['value_send'];
    $store_id = $value_received;
    
    // update outlink hit count
    global $wpdb;
    $tb_name = $wpdb->prefix.'outlink';
    cpx_update_outlink_hit_count($tb_name, 's'.$store_id);
    
    echo get_post_meta($store_id,'aff_url_store_metadata',true);
    die();
}
function show_detail_code($id){  
    global $wpdb,$table_prefix;
    $my_query = "SELECT * FROM ".$table_prefix."posts WHERE ID='$id'";
    $coupon = $wpdb->get_results($my_query);
    foreach ($coupon as $v){
            $cp_id = $v->ID;
            $coupon_type = get_post_meta($cp_id,'coupon_type_metadata',true);
            $coupon_code = get_post_meta($cp_id, 'coupon_code_metadata', true);
            $store_id = get_post_meta($cp_id, 'store_coupon_metadata', true);
            $store_outlink = cpx_get_outlink($store_id);
            $store_name = get_the_title($store_id);
        ?>
                        <div id="popup-bg-active"></div>   
                                <div id="active_popup">
                                    <div class="row-fluid pophead">
                                        <div class="span10">
                                             <p class="poptitle">Here's your <?php echo $store_name;?> code:</p>
                                        </div>
                                        <div class="span2">
                                            <span class="modal_close_cp" href="#"></span>
                                        </div>
                                    </div>
                                    <?php
                                        if($coupon_type == 'promotion'){ ?>
                                                <div class="row-fluid current-offer">
                                                    <p class="poptitle">No coupon code required!</p>
                                                </div>
                                        <?php }else{ if($coupon_code ==''){ ?>
                                                <div class="row-fluid current-offer">
                                                    <p class="poptitle">No coupon code required!</p>
                                                </div>
                                        <?php }else{ ?>
                                                <div class="row-fluid current-offer">
                                                        <input name="coupon-code" disabled="disabled" type="text" value="<?php echo $coupon_code;?>"/>
                                                        <button name="copycode" type="submit">Copy</button>
                                                </div>
                                           <?php }
                                        }?>
                                            <div class="row-fluid popinmess">
                                            <?php if($coupon_code): ?>
                                                <p>Paste this code at checkout when you are done shopping.</p>
                                            <?php endif; ?>
                                                <a href="<?php echo $store_outlink; ?>" target="_blank"><?php echo "Shop now at {$store_name} website to save"; ?></a>
                                                <p><a href="<?php echo get_permalink($store_id); ?>" target="_blank"><?php echo "View more coupons at {$store_name} store"; ?></a></p>
                                            </div>
                                    <div class="row-fluid popvote">
                                        <?php cpx_vote_coupon($cp_id, true); ?>
                                        <div class="span10">
                                            <div class="row-fluid">
                                                <p class="vopdt"><?php echo $v->post_title;?></p>
                                            </div>
                                            <div class="row-fluid">
                                                <p class="vopds">
                                                    <?php echo $store_name;?>: <?php echo $v->post_content;?>
                                                </p>
                                            </div>
                                            <div class="row-fluid vopcoms">
                                                <span class="vopcom count"><?php echo $v->comment_count?></span>
                                                <span class="showcm-pop cm-count">Comment</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row-fluid cm_form-pop display-none">
                                        <?php
                                            global $current_user;
                                            if(!$current_user){
                                                $user_id ='';
                                                $user_name = 'Anonymous';
                                                $user_email ='';
                                                $user_url ='';
                                            }else{
                                                $user_id = $current_user->ID;
                                                $user_name =$current_user->user_login;
                                                $user_email = $current_user->user_email;
                                                $user_url = $current_user->user_url;
                                            }
                                           $comment_author_IP = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
                                           $comment_agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
                                           $comment_date = current_time('mysql');
                                           $comment_date_gmt = current_time('mysql', 1);
                                        ?>
                                        <div id="respond">
                                            <h3 id="reply-title">
                                            Add Comment 
                                            </h3>
                                            <form action="" method="post" id="commentformpop" name="comment-post">
                                                <p class="comment-form-comment">
                                                    <textarea class="commentTxt" id="comment" name="comment_content-pop" aria-required="true"></textarea>
                                                </p>
                                                <p class="form-submit">
                                                    <input name="comment_action" type="submit" id="submit_comment" value="Post comment">
                                                    <input type="hidden" name="comment_post_ID-pop" value="<?php echo $cp_id;?>" id="comment_post_ID">
                                                    <input type="hidden" name="comment_author_IP-pop" id="comment_author_IP" value="<?php echo $comment_author_IP;?>">
                                                    <input type="hidden" name="comment_agent-pop" id="comment_agent" value="<?php echo $comment_agent;?>">
                                                    <input type="hidden" name="comment_date-pop" id="comment_date" value="<?php echo $comment_date;?>">
                                                    <input type="hidden" name="comment_date_gmt-pop" id="comment_date_gmt" value="<?php echo $comment_date_gmt;?>">
                                                    <input type="hidden" name="user_id-pop" id="user_id" value="<?php echo $user_id;?>">
                                                    <input type="hidden" name="comment_author-pop" id="comment_author" value="<?php echo $user_name;?>">
                                                    <input type="hidden" name="comment_author_email-pop" id="comment_author_email" value="<?php echo $user_email;?>">
                                                    <input type="hidden" name="user_url-pop" id="user_url" value="<?php echo $user_url;?>">
                                                </p>
                                            </form>
                                        </div>
                                        <ol class="commentlist">
                                             <?php
                                                    $comments = get_comments(array(
                                                            'post_id' => $cp_id,
                                                            'status' => 'approve'
                                                    ));
                                                    wp_list_comments(array(
                                                            'per_page' => -1,
                                                            'reverse_top_level' => false,
                                                            'reply_text' => false,
                                                            'callback' => 'bigsave_comment'
                                                       ), 
                                                       $comments);
                                            ?>
                                        </ol>
                                    </div>
                            </div>
    <?php
    }
}
?>